/**
 * Title: WelcomActivity.java
 * Description:
 * author: kotomifi
 * date: Jul 6, 2014
 * ©2014 apping 社区
 */
package com.apping.demo.bbs;

import java.util.ArrayList;

import com.sina.weibo.sdk.demo.R;
import com.apping.demo.bbs.adapter.WelcomeAdapter;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

/**
 *
 */
public class WelcomeActivity extends Activity {
	
	private ViewPager mViewPager;
	
	private ImageView m_page0;
	private ImageView m_page1;
	private ImageView m_page2;
	
	private Button startBtn;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.welcome_activity);
		
		// 隐藏活动条
		ActionBar actionBar = getActionBar();
		actionBar.hide();
		
		mViewPager = (ViewPager)findViewById(R.id.welcome_viewpager);
		mViewPager.setOnPageChangeListener(new WelcomeOnPageChangeListener());
		// 获取滑动圆点
		m_page0 = (ImageView)findViewById(R.id.page0);
		m_page1 = (ImageView)findViewById(R.id.page1);
		m_page2 = (ImageView)findViewById(R.id.page2);
		
		LayoutInflater m_inflater = LayoutInflater.from(this);
		View view0 = m_inflater.inflate(R.layout.welcome_page_0, null);
		View view1 = m_inflater.inflate(R.layout.welcome_page_1, null);
		View view2 = m_inflater.inflate(R.layout.welcome_page_2, null);
		
		final ArrayList<View> views = new ArrayList<View>();
		views.add(view0);
		views.add(view1);
		views.add(view2);
		
		final ArrayList<String> titles = new ArrayList<String>();
		titles.add("tab0");
		titles.add("tab1");
		titles.add("tab2");
		
		WelcomeAdapter welcomAdapter = new WelcomeAdapter(views, titles);
		mViewPager.setAdapter(welcomAdapter);
	}
	
	public void initStartBtn() {
		startBtn = (Button)findViewById(R.id.welcome_start_btn);
		startBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(WelcomeActivity.this, LoginActivity.class);
				startActivity(intent);
			}
		});
	}
	
	public class WelcomeOnPageChangeListener implements OnPageChangeListener {

		/* (non-Javadoc)
		 * @see android.support.v4.view.ViewPager.OnPageChangeListener#onPageScrollStateChanged(int)
		 */
		@Override
		public void onPageScrollStateChanged(int arg0) {
			// TODO Auto-generated method stub
			
		}

		/* (non-Javadoc)
		 * @see android.support.v4.view.ViewPager.OnPageChangeListener#onPageScrolled(int, float, int)
		 */
		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			// TODO Auto-generated method stub
			
		}

		/* (non-Javadoc)
		 * @see android.support.v4.view.ViewPager.OnPageChangeListener#onPageSelected(int)
		 */
		@Override
		public void onPageSelected(int page) {
			// TODO Auto-generated method stub
			switch(page) {
			case 0:
				m_page0.setImageDrawable(getResources().getDrawable(R.drawable.page_now));
				m_page1.setImageDrawable(getResources().getDrawable(R.drawable.page));
				break;
			case 1:
				m_page1.setImageDrawable(getResources().getDrawable(R.drawable.page_now));
				m_page0.setImageDrawable(getResources().getDrawable(R.drawable.page));
				m_page2.setImageDrawable(getResources().getDrawable(R.drawable.page));
				break;
			case 2:
				m_page2.setImageDrawable(getResources().getDrawable(R.drawable.page_now));
				m_page1.setImageDrawable(getResources().getDrawable(R.drawable.page));
				initStartBtn();
				break;
			}
		}
		
	}

}
